package com.zhouheng.springbootdesignpattern.archetypalpattern;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 测试原型模式
 * -浅克隆
 * -深克隆
 *
 * @author 周恒
 * @date 20190329 17:42:40
 * @since v1.0
 */
public class Client {

    public static void main(String[] args) {
        System.out.println("====================================浅克隆 Start====================================");
        System.out.println();
        prototypeShallowClone();
        System.out.println();
        System.out.println("====================================浅克隆 End====================================");

        System.out.println();

        System.out.println("====================================深克隆 Start====================================");
        System.out.println();
        prototypeDeepClone();
        System.out.println();
        System.out.println("====================================深克隆 End====================================");
    }

    /**
     * 原型模式-浅克隆
     * 只会克隆java8中定义的基本类型，以及其包装类，还有String类型
     * 对引用类型会使用同一份数据
     *
     * @author 周恒
     * @date 20190329 17:42:40
     * @since v1.0
     */
    private static void prototypeShallowClone() {
        List<String> interest = new ArrayList<>(Arrays.asList("运动", "音乐", "读书"));
        Car car = new Car("保时捷");
        //原值与克隆值
        System.out.println("=============================原值和克隆值=============================");
        Shallow shallow = new Shallow("淑芬", 23, true, interest, car);
        System.out.println("source:" + shallow.toString());
        Shallow clone = shallow.clone();
        System.out.println("target:" + clone.toString());
        System.out.println();

        //修改原值
        System.out.println("=============================修改原值=============================");
        shallow.setAge(38);
        shallow.getCar().setName("法拉利");
        List<String> interest1 = shallow.getInterest();
        interest1.add("男人");
        System.out.println("source:" + shallow.toString());
        System.out.println("target:" + clone.toString());
        System.out.println();

        //修改克隆值
        System.out.println("=============================修改克隆值=============================");
        clone.setMarriage(false);
        clone.getCar().setName("保时捷911");
        List<String> cloneGirlInterest = clone.getInterest();
        cloneGirlInterest.add("女人");
        System.out.println("source:" + shallow.toString());
        System.out.println("target:" + clone.toString());
    }

    /**
     * 原型模式-深克隆
     * 引用类型，是分开不同的数据
     *
     * @author 周恒
     * @date 20190329 17:42:40
     * @since v1.0
     */
    private static void prototypeDeepClone() {

        ArrayList<String> interest = new ArrayList<>(Arrays.asList("电竞", "爬山", "乒乓球"));
        Car car = new Car("兰博基尼");
        //原值与克隆值
        System.out.println("=============================原值和克隆值=============================");
        Deep deep = new Deep("老王", 100000, true, interest, car);
        System.out.println("source:" + deep.toString());
        Deep clone = deep.clone();
        System.out.println("target:" + clone.toString());
        System.out.println();

        //修改原值
        System.out.println("=============================修改原值=============================");
        deep.setMoney(1999);
        deep.getCar().setName("睿蓝汽车");

        List<String> interest1 = deep.getInterest();
        interest1.add("女人");
        System.out.println("source:" + deep.toString());
        System.out.println("target:" + clone.toString());
        System.out.println();

        //修改克隆值
        System.out.println("=============================修改克隆值=============================");
        clone.setMarriage(false);
        clone.getCar().setName("吉利汽车");
        List<String> cloneBoyInterest = clone.getInterest();
        cloneBoyInterest.add("男人");
        System.out.println("source:" + deep.toString());
        System.out.println("target:" + clone.toString());
    }

}
